package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.dto.iapDataModel.IapDataModelDTO;
import com.etone.smartAudit.enums.UseState;
import com.etone.smartAudit.service.IapDataModelService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.iapDataModel.IapDataModelBaseVO;
import com.etone.smartAudit.vo.iapDataModel.IapDataModelDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: sovy
 * @date: 2020/8/28
 * @description:
 */
@RestController
@AllArgsConstructor
@Api(tags = "元数据模型", value = "IapDataModelController")
public class IapDataModelController {
    private IapDataModelService iapDataModelService;

    @ApiOperation(value = "元数据列表《完》")
    @GetMapping("/iapDataModels")
    public PagerVO<IapDataModelBaseVO> list(
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "系统名称") @RequestParam(required = false) String iapPlatformName,
            @ApiParam(value = "宽表表名") @RequestParam(required = false) String cnTableName,
            @ApiParam(value = "英文表名") @RequestParam(required = false) String enTableName,
            @ApiParam(value = "说明备注") @RequestParam(required = false) String description,
            @ApiParam(value = "二级分类id") @RequestParam(required = false) List<Integer> level3Ids,
            @ApiParam(value = "数据库名称") @RequestParam(required = false) String databaseName,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "状态") @RequestParam(required = false) UseState useState,
            @ApiParam(value = "敏感等级Id") @RequestParam(required = false) Integer senLevel,
            @ApiParam(value = "数据存放平台Id") @RequestParam(required = false) Integer dataPssA,
            @ApiParam(value = "数据维护标签id集合") @RequestParam(required = false) List<Integer> dataAssertIds,
            @ApiParam(value = "质量维度标签id集合") @RequestParam(required = false) List<Integer> dataMassIds,
            @ApiParam(value = "使用方维度标签id集合") @RequestParam(required = false) List<Integer> dimensionIds,
            @ApiParam(value = "自动化维度标签id集合") @RequestParam(required = false) List<Integer> autoTagIds,
            @ApiParam(value = "数据时效开始时间") @RequestParam(required = false) Date startTime,
            @ApiParam(value = "数据时效结束时间") @RequestParam(required = false) Date endTime
    ) {
        return iapDataModelService.list(iapPlatformName, cnTableName, enTableName, description, level3Ids, databaseName, itemId, useState, page, pageSize,senLevel,dataPssA,startTime,endTime,dataAssertIds,dataMassIds,dimensionIds,autoTagIds);
    }

    @AdminOperation(module = "元数据查询", item = "添加")
    @ApiOperation(value = "增加元数据《完》")
    @PostMapping("/iapDataModels")
    public void add(
            @ApiParam("元数据表单") @RequestBody @Validated IapDataModelDTO iapDataModelDTO
    ) {
        iapDataModelService.save(null, iapDataModelDTO);
    }

    @AdminOperation(module = "元数据查询", item = "编辑")
    @ApiOperation(value = "编辑元数据《完》")
    @PutMapping("/iapDataModels/{id}")
    public void edit(
            @ApiParam("id") @PathVariable Integer id,
            @ApiParam("元数据表单") @RequestBody @Validated IapDataModelDTO iapDataModelDTO
    ) {
        iapDataModelService.save(id, iapDataModelDTO);
    }

    @ApiOperation(value = "查询英文表名是否存在《完》")
    @GetMapping("/iapDataModels/enTableName/exists")
    public Boolean checkEnTableNameExists(
            @ApiParam(value = "元数据id(编辑时判断是否是自身)") @RequestParam(required = false) Integer id,
            @ApiParam(value = "系统id", required = true) @RequestParam Integer platformId,
            @ApiParam(value = "英文表名", required = true) @RequestParam String enTableName
    ) {
        return iapDataModelService.checkEnTableNameExists(id, platformId, enTableName);
    }

    @ApiOperation(value = "查询宽表表名是否存在《完》")
    @GetMapping("/iapDataModels/cnTableName/exists")
    public Boolean checkCnTableNameExists(
            @ApiParam(value = "元数据id(编辑时判断是否是自身)") @RequestParam(required = false) Integer id,
            @ApiParam(value = "系统id", required = true) @RequestParam Integer platformId,
            @ApiParam(value = "宽表表名", required = true) @RequestParam String cnTableName
    ) {
        return iapDataModelService.checkCnTableNameExists(id, platformId, cnTableName);
    }

    @ApiOperation(value = "元数据详情《完》")
    @GetMapping("/iapDataModels/{id}")
    public IapDataModelDetailVO detail(
            @ApiParam("id") @PathVariable Integer id
    ) {
        return iapDataModelService.detail(id);
    }

    @ApiOperation(value = "数据模型真实数据列表《完》")
    @GetMapping("/iapDataModels/{id}/datas")
    public List<Map<String, Object>> metaDataTop(
            @ApiParam("id") @PathVariable Integer id,
            @ApiParam("top:默认5") @RequestParam(required = false, defaultValue = "5") Integer top
    ) {
        return iapDataModelService.metaDataTop(id, top);
    }

    @AdminOperation(module = "元数据查询", item = "删除")
    @ApiOperation(value = "删除元数据《完》")
    @DeleteMapping("/iapDataModels/{id}")
    public void delete(
            @ApiParam("id") @PathVariable Integer id
    ) {
        iapDataModelService.delete(id);
    }

    @AdminOperation(module = "元数据查询", item = "批量删除")
    @ApiOperation(value = "批量删除元数据《完》")
    @DeleteMapping("/iapDataModels/batch/{ids}")
    public void batchDelete(
            @ApiParam("ids") @PathVariable List<Integer> ids
    ) {
        iapDataModelService.batchDelete(ids);
    }

    @AdminOperation(module = "元数据查询", item = "批量导入")
    @ApiOperation(value = "批量导入元数据《完》")
    @PostMapping("/iapDataModels/import")
    public void importDataModel(
            @ApiParam(value = "excel文件", required = true) @RequestPart MultipartFile file
    ) {
        iapDataModelService.importDataModel(file);
    }
}
